const activityModel = require('../model/activityModel.js')
const informationModel = require("../model/informationModel");
const activityController ={
    GetActivityList: async (req, res) => {
        console.log(req.body,'controller层获取活动列表调试');
        try {
            const {list,total} = await activityModel.GetActivityList(req.body);
            console.log(list,total,'活动列表响应参数');
            if(list.length > 0){
                res.status(200).send({
                    code: 1,
                    msg: "成功获取活动列表",
                    list,
                    total,
                });
            }else{
                res.status(404).send({
                    code: 0,
                    msg: "获取活动列表失败，请输入正确的参数",
                });
            }
        } catch (error) {
            console.log(error,"获取活动列表出现error");
        };
    },
    AddActivity: async (req, res) => {
        try {
            const result = await activityModel.AddActivity(req.body,req.file);
            if(result == 0){
                res.send({
                    code:0,
                    msg:"该活动标题已存在，请重新添加！"
                });
            }else if(result == 1){
                res.send({
                    code:1,
                    msg:"添加活动成功！"
                });
            }else if(result.length == 0){
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'添加活动出错啦！');
        }
    },
    EditActivity: async (req,res) => {
        console.log('修改活动参数修改')
        try {
            const result = await activityModel.EditActivity(req.body,req.file);
            if(result === 1){
                res.send({
                    code:1,
                    msg:"修改活动成功！"
                });
            }else {
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'修改汽车出错啦！');
        }
    },
    DeleteActivity: async (req, res) => {
        try {
            const result = await activityModel.DeleteActivity(req.body);
            if (result) {
                res.status(200).send({
                    code: 1,
                    msg: "删除活动成功",
                });
            } else {
                res.status(404).send({
                    code: 0,
                    msg: "删除活动失败",
                });
            }
        } catch (error) {
            console.log(error, '删除活动出现error');
        };
    },
    //批量删除
    BatchDeleteActivity: async (req,res)=>{
        try {
            let result = await activityModel.BatchDeleteActivity(req.body);
            if(result.affectedRows !== 0){
                res.status(200).send({
                    code:1,
                    msg:"批量删除活动成功",
                });
            }else{
                res.status(401).send({
                    code:0,
                    msg:"批量删除活动失败",
                });
            }
        }catch (error){
            console.log(error,'controller层批量删除活动出现error');
        }
    },
    UpdateActivityState: async (req,res)=>{
        try {
            const result = await activityModel.UpdateActivityState(req.body);
            if(result){
                res.status(200).send({
                    code:1,
                    msg:"更新活动举办状态成功",
                });
            }else{
                res.status(404).send({
                    code:0,
                    msg:"更新活动举办状态失败",
                });
            }
        } catch (error) {
            console.log(error,'更新活动发布状态成功出现error');
        }
    },
    GetActivityDetail:async (req,res) => {
        try{
            const result = await activityModel.GetActivityDetail(req.params);
            console.log(result,'activity-controller结果');
            if(result.length >0){
                res.status(200).send({
                    code:1,
                    detail:result[0],
                    msg:"获取活动详情成功"
                })
            }else{
                res.status(404).send({
                    code:0,
                    msg:"获取活动详情失败",
                });
            }
        }catch (error){
            console.log(error,'获取活动详情出现error');
        }
    },
    GetActivityPublishList:async (req,res) => {
        console.log(req.body,'controller层获取发布活动列表调试');
        try {
            const {list,total} = await activityModel.GetActivityPublishList(req.body);
            console.log(list,total,'发布活动列表响应参数');
            if(list.length > 0){
                res.status(200).send({
                    code: 1,
                    msg: "成功获取已发布活动列表",
                    list,
                    total,
                });
            }else{
                res.status(404).send({
                    code: 0,
                    msg: "获取已发布活动列表失败，请输入正确的参数",
                });
            }
        } catch (error) {
            console.log(error,"获取已发布活动列表出现error");
        };
    },

};


module.exports = activityController;